﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using System.Data;
using CommonData;
using DataCentral;
using DataCentral.Dataset;
using DataCentral.Forms;

namespace DataCentral.UsecaseControllers
{
    public class SalePersonInfo
    {
        public decimal SalePersonID
        {
            get;
            set;
        }
        public string SalePerTye
        {
            get;
            set;
        }

        public string SalePersonCode
        {
            get;
            set;
        }

        public string SalePersonName
        {
            get;
            set;
        }

        public decimal SaleTeamID
        {
            get;
            set;
        }

        //{0}.<{1}> {2}-{3}
        public SalePersonInfo(string strFull,decimal refSaleTeamID)
        {
            SalePersonID = Convert.ToDecimal(strFull.Substring(0, strFull.IndexOf('.')).Trim());
            SalePerTye = strFull.Substring(strFull.IndexOf('<') + 1, strFull.IndexOf('>') - strFull.IndexOf('<') -1).Trim();
            SalePersonCode = strFull.Substring(strFull.IndexOf('>') + 1, strFull.IndexOf('-') - strFull.IndexOf('>') - 1).Trim();
            SalePersonName = strFull.Substring(strFull.IndexOf('-') + 1, strFull.Length - strFull.IndexOf('-') - 1).Trim();
            SaleTeamID = refSaleTeamID;
        }
    }

    public class AssignSalePersonUsecase
    {
        public AssignSalePersonUsecase()
        {
            
        }



        public void CreateSaleTeamList(ImageListBoxControl imlSaleTeam)
        {
            ValueListClass clValueList = new ValueListClass();
            DataTable tblSaleTeam = clValueList.GetSaleTeamList();
            imlSaleTeam.Items.Clear();
            for(int i =0;i<tblSaleTeam.Rows.Count;i++)
            {
                DataCentralDataset.SaleTeamRow drSaleTeam = (DataCentralDataset.SaleTeamRow)tblSaleTeam.Rows[i];
                imlSaleTeam.Items.Add(string.Format("{0}.{1}-{2}", drSaleTeam.SlsTeamID, drSaleTeam.SlsTeamCode, drSaleTeam.Descr),2); 
            }
        }

        public void CreateSysSlsPersonList(decimal saleTeamID,ImageListBoxControl imlSysPerson,ImageListBoxControl imlSalePerson)
        {
            CommonDataAccess cmData = new CommonDataAccess();
            ValueListClass clValueList = new ValueListClass();
            DataTable tblSysPerson = cmData.GetDataTableBySproc("DATA_GetUnAssignSalePerson", null);

            List<ParameterObject> lstParams = new List<ParameterObject>();
            lstParams.Add(new ParameterObject("@SlsTeamID", saleTeamID));
            DataTable tblSalePerson = cmData.GetDataTableBySproc("DATA_GetSalePersonBySaleTeam",lstParams);

            imlSysPerson.Items.Clear();
            imlSalePerson.Items.Clear();
            for (int i = 0; i < tblSysPerson.Rows.Count; i++)
            {
                if (tblSalePerson.Select("SalePersonID = " + tblSysPerson.Rows[i]["SalePersonID"]).Length == 0)
                {
                    //Add sysperson
                    imlSysPerson.Items.Add(string.Format("{0}.<{1}> {2}-{3}", tblSysPerson.Rows[i]["SalePersonID"], tblSysPerson.Rows[i]["SlsperType"], tblSysPerson.Rows[i]["SlsperCode"], tblSysPerson.Rows[i]["Name"]), tblSysPerson.Rows[i]["SlsperType"].ToString().Equals("NVBH") ? 0 : 1);
                }
            }

            for (int i = 0; i < tblSalePerson.Rows.Count; i++)
            {
                imlSalePerson.Items.Add(string.Format("{0}.<{1}> {2}-{3}", tblSalePerson.Rows[i]["SalePersonID"], tblSalePerson.Rows[i]["SlsperType"], tblSalePerson.Rows[i]["SlsperCode"], tblSalePerson.Rows[i]["Name"]), tblSalePerson.Rows[i]["SlsperType"].ToString().Equals("NVBH") ? 0 : 1);
            }
        }

    }
}
